<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class BaseDB
{
    /**
     * 基本DB
     */

    /**
     * JSON转化为数组
     */
    public function jsonToArr($str) {
        if (is_string($str)) $str = json_decode($str);
        $arr = array();
        foreach($str as $k=>$v) {
            if(is_object($v) || is_array($v))
                $arr[$k] = $this->jsonToArr($v);
            else
                $arr[$k] = $v;
        }
        return $arr;
    }

    /**
     * 文件大小分级
     */
    public function getSizeLevel($size)
    {
        if ($size>1024 && $size<=1024*1024) {
            return round($size/1024,2) . 'KB';
        }
        else if ($size>1024*1024 && $size<=1024*1024*1024) {
            return round($size/1024/1024,2) . 'MB';
        }
        else if ($size>1024*1024*1024) {
            return round($size/1024/1024/1024,2) . 'GB';
        }
        else {
            return $size . 'B';
        }
    }
}